package com.bole.controller;


import com.bole.dao.CarSeriesDao;
import com.bole.dto.brand.TbCarSeriesDto;
import com.bole.pojo.TbCarSeriesDo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("series")
@Api(value = "车系管理api")
public class CarSeriesController {

    @Autowired
    private CarSeriesDao carSeriesDao;

    @GetMapping("getCarSeriesInfo")
    @ApiOperation(value = "查询全部车系",notes = "查询全部车系")
    public List<TbCarSeriesDto> getCarSeriesInfo() {
        List<TbCarSeriesDo> all = carSeriesDao.findAll();
        ArrayList<TbCarSeriesDto> arrayList = new ArrayList<>();
        for (TbCarSeriesDo tbCarSeriesDo : all) {
            TbCarSeriesDto contentTypeDto = new TbCarSeriesDto();
            BeanUtils.copyProperties(tbCarSeriesDo, contentTypeDto);
            arrayList.add(contentTypeDto);
        }

        return arrayList;

    }

    @PostMapping("saveCarSeriesInfo")
    @ApiOperation(value = "保存车系",notes = "保存车系")
    @ApiImplicitParam(name = "carSeriesDo",value = "车系对象",required = true,dataTypeClass = TbCarSeriesDo.class)
    public void saveCarSeriesInfo(@RequestBody TbCarSeriesDo carSeriesDo) {
        TbCarSeriesDo tbCarSeriesDo = carSeriesDao.save(carSeriesDo);
        TbCarSeriesDto contentTypeDto = new TbCarSeriesDto();
        BeanUtils.copyProperties(tbCarSeriesDo, contentTypeDto);
    }

    @PostMapping("toUpdate")
    @ApiOperation(value = "回显",notes = "回显")
    @ApiImplicitParam(name = "id",value = "车系id",required = true,dataTypeClass = Long.class)
    public TbCarSeriesDto toUpdate(@RequestParam("id") Long id) {
        TbCarSeriesDo tbCarSeriesDo = carSeriesDao.findById(id).get();
        TbCarSeriesDto contentTypeDto = new TbCarSeriesDto();
        BeanUtils.copyProperties(tbCarSeriesDo, contentTypeDto);
        return contentTypeDto;
    }

    @PostMapping("updateCarSeriesInfo")
    @ApiOperation(value = "修改信息",notes = "修改信息")
    @ApiImplicitParam(name = "carSeriesDo",value = "车系对象",required = true,dataTypeClass = TbCarSeriesDo.class)
    public void updateContentCateInfo(@RequestBody TbCarSeriesDo carSeriesDo) {
        TbCarSeriesDo tbCarSeriesDo = carSeriesDao.save(carSeriesDo);
        TbCarSeriesDto contentTypeDto = new TbCarSeriesDto();
        BeanUtils.copyProperties(tbCarSeriesDo, contentTypeDto);
    }

    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除",notes = "批量删除")
    @ApiImplicitParam(name = "idss",value = "车系对象",required = true,dataTypeClass = Long.class)
    public void deleteBatch(@RequestParam("idss") List<Long> idss) {
        carSeriesDao.deleteBatch(idss);
    }



    //通过品牌id查询
    @PostMapping("selectByBrandId")
    @ApiOperation(value = "品牌id查询",notes = "品牌id查询")
    @ApiImplicitParam(name = "brandId",value = "品牌ID",required = true,dataTypeClass = Long.class)
    public List<TbCarSeriesDto> selectByBrandId(@RequestParam("brandId") Long brandId) {
        List<TbCarSeriesDo> carSeriesDos = carSeriesDao.findAllByBrandId(brandId);
        ArrayList<TbCarSeriesDto> carSeriesDtos = new ArrayList<>();
        for (TbCarSeriesDo tbCarSeriesDo : carSeriesDos) {
            TbCarSeriesDto contentTypeDto = new TbCarSeriesDto();
            BeanUtils.copyProperties(tbCarSeriesDo, contentTypeDto);
            carSeriesDtos.add(contentTypeDto);
        }
        return carSeriesDtos;
    }
}
